﻿page_SmeIncentive = new function () {
    Number.prototype.format = function (n, x, s, c) {
        var re = '\\d(?=(\\d{' + (x || 3) + '})+' + (n > 0 ? '\\D' : '$') + ')',
        num = this.toFixed(Math.max(0, ~ ~n));

        if (num == 0) return "-";

        return (c ? num.replace('.', c) : num).replace(new RegExp(re, 'g'), '$&' + (s || ',')).replace('.00', '');
    };

    this.documentReady = function () {
        this.binOverview();
        this.bindIncentiveByMonth();

        this.bindIncentiveTopSaleman("Point Incentive");
        this.bindIncentiveTopSaleman("Increasing Point Incentive");
    };

    $("#ctl00_ContentPlaceHolder1_ddlMonth").live('change', function () {
        page_SmeIncentive.binOverview();
        page_SmeIncentive.bindIncentiveTopSaleman("Point Incentive");
        page_SmeIncentive.bindIncentiveTopSaleman("Increasing Point Incentive");
    });

    this.binOverview = function () {
        year = $("#ddlYear").val();
        month = $("#ctl00_ContentPlaceHolder1_ddlMonth").val();

        $.ajax({
            type: "GET",
            url: "/handler/Dashboard.ashx",
            data: { year: year, month: month, t: 'SmeIncentiveOverview' },
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            cache: false,
            success: function (data) {
                if (data != null) {
                    var incentive = '';
                    var salesman = '';
                    var eligibleSalesman = '';

                    $("#overview1").html(data.YTMIncentive.format(2, 3, ',', '.'));
                    if (data.Incentive > 0)
                        incentive = '<img src=""/>&nbsp;' + data.Incentive.format(2, 3, ',', '.');
                    else incentive = '<img src=""/>&nbsp;' + data.Incentive * (-1);

                    $("#overview2").html(incentive);

                    if (data.Salesman > 0)
                        salesman = '<img src=""/>&nbsp;' + data.Salesman.format(2, 3, ',', '.');
                    else salesman = '<img src=""/>&nbsp;' + data.Salesman * (-1);

                    $("#overview3").html(salesman);

                    if (data.EligibleSalesman > 0)
                        eligibleSalesman = '<img src=""/>&nbsp;' + data.EligibleSalesman.format(2, 3, ',', '.');
                    else eligibleSalesman = '<img src=""/>&nbsp;' + data.EligibleSalesman * (-1);

                    $("#overview4").html(eligibleSalesman);
                }
            }
        });
    };

    this.bindIncentiveByMonth = function () {
        year = $("#ddlYear").val();
        $.ajax({
            type: "GET",
            url: "/handler/Dashboard.ashx",
            data: { year: year, t: "SmeIncentiveByMonth" },
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            cache: false,
            success: function (data) {
                $("#tbIncentiveByMonth").setTemplateURL("/templates/pages/dashboard/sme-incentive/by-month.htm");
                $("#tbIncentiveByMonth").processTemplate(data);

                $(".percent").append('%');
            }
        });
    };

    this.bindIncentiveTopSaleman = function (by) {
        year = $("#ddlYear").val();
        month = $("#ctl00_ContentPlaceHolder1_ddlMonth").val();
        var wrapper = "#tbTopByPoint";
        $.ajax({
            type: "GET",
            url: "/handler/Dashboard.ashx",
            data: { year: year, month: month, by: by, t: "SmeIncentiveTopSaleman" },
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            cache: false,
            success: function (data) {
                if (by == "Increasing Point Incentive")
                    wrapper = "#tbTopByIncreasingPoint";

                $(wrapper).setTemplateURL("/templates/pages/dashboard/sme-incentive/top-saleman.htm");
                $(wrapper).processTemplate(data);
            }
        });
    };

    this.getClass = function (index, value) {
        if (index==3 && value > 0) return 'percent';
    };
};